package com.gc.web.controller.business;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.gc.business.domain.AdminRegion;
import com.gc.business.domain.GovRegion;
import com.gc.business.service.IAdminRegionService;
import com.gc.business.service.IGovRegionService;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import com.gc.common.utils.IdUtils;
import com.gc.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.GovManage;
import com.gc.business.service.IGovManageService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 政府机构管理 控制层
 * 
 * @author hruiyan
 * @date 2020-11-12
 */
@Slf4j
@Controller
@RequestMapping("/business/govManage")
public class GovManageController extends BaseController {
    private String prefix = "business/govManage";

	/**
	 * 政府机构
	 */
	private final IGovManageService govManageService;
	/**
	 * 行政区域
	 */
	private final IAdminRegionService adminRegionService;
	/**
	 * 政府机构管辖行政区域中间表
	 */
	private final IGovRegionService govRegionService;

	@Autowired
	public GovManageController(IGovManageService govManageService,
							   IAdminRegionService adminRegionService,
										   IGovRegionService govRegionService) {
		this.govManageService = govManageService;
		this.adminRegionService = adminRegionService;
		this.govRegionService = govRegionService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:govManage:view")
	@GetMapping
	public String govManage(){
	    return prefix + "/govManage";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap){
		List<AdminRegion> regionList = adminRegionService.selectAdminRegion();
		mmap.put("regionList",regionList);
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap){
		GovManage govManage = govManageService.getById(id);
		List<AdminRegion> regionList = govRegionService.selectGovRegionByGovId(govManage.getGovId());
		mmap.put("regionList",regionList);
		mmap.put("govManage", govManage);
		return prefix + "/edit";
	}
	
	/**
	 * 查询政府机构管理列表
	 */
	@RequiresPermissions("business:govManage:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(GovManage govManage){
		startPage();
        List<GovManage> list = govManageService.list(govManage);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存政府机构管理
	 */
	@RequiresPermissions("business:govManage:add")
	@Log(title = "政府机构管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Result addSave(GovManage govManage,String regionList){
		govManage.setGovId(IdUtils.getPrimaryKey());
		govManage.setCreateTime(new Date());
		govManage.setCreateBy(ShiroUtils.getLoginName());
		govManageService.save(govManage);
		this.saveGovRegion(govManage.getGovId(),regionList);
		return Result.success();
	}

	/**
	 * 修改保存政府机构管理
	 */
	@RequiresPermissions("business:govManage:edit")
	@Log(title = "政府机构管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Result editSave(GovManage govManage,String regionList){
		govManageService.updateById(govManage);
		//重置保存过的行政区域数据
		govRegionService.deleteGovRegionByGovId(govManage.getGovId());
		this.saveGovRegion(govManage.getGovId(),regionList);
		return Result.success();
	}
	
	/**
	 * 删除政府机构管理
	 */
	@RequiresPermissions("business:govManage:remove")
	@Log(title = "政府机构管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		govManageService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出政府机构管理列表
	 */
	@RequiresPermissions("business:govManage:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(GovManage govManage){
		List<GovManage> list = govManageService.list(govManage);
		ExcelUtil<GovManage> util = new ExcelUtil<>(GovManage.class);
		util.exportExcel(list, "govManage");
		return Result.success();
	}

	/**
	 * 保存政府管辖行政区域中间
	 * @param govId
	 * @param regionList
	 */
	private void saveGovRegion(Long govId,String regionList){
		//保存政府管辖行政区域中间
		Long[] regionArray = Convert.toLongArray(regionList);
		GovRegion govRegion = null;
		for (Long regionId : regionArray) {
			govRegion = new GovRegion();
			govRegion.setGovId(govId);
			govRegion.setRegionId(regionId);
			govRegionService.save(govRegion);
		}
	}
}
